package com.aurora.oa.home.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.aurora.oa.R;
import com.aurora.oa.home.adapter.ArticleAdapter;
import com.aurora.oa.home.bean.ArticleBean;
import com.aurora.oa.home.presenter.ArticalListActivityPresenter;
import com.aurora.oa.home.presenter.ArticalListActivityPresenterImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/18 0018.
 */
public class ArticalListActivity extends AppCompatActivity implements ArticalListActivityView{

    Toolbar toolbar;
    RecyclerView recyclerView;
    SwipeRefreshLayout swipeRefreshLayout;
    ArticalListActivityPresenter articalListActivityPresenter;
    LinearLayoutManager layoutManager;
    boolean isLoading;
    private int whitchPage =1;
    private List<ArticleBean> data = new ArrayList<>();
    private ArticleAdapter adapter = new ArticleAdapter(this, data,true);
    private Handler handler = new Handler();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_artical_list);
        initTitleBar();
        initView();
        //   initData();
    }

    private void initTitleBar() {
        toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar ab =getSupportActionBar();
        //把原始标题置空，显示自己的居中标题
        ab.setTitle("");
        ab.setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    private void initView() {

        articalListActivityPresenter = new ArticalListActivityPresenterImpl(this);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.SwipeRefreshLayout);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        swipeRefreshLayout.setColorSchemeResources(R.color.blue,R.color.white);
        articalListActivityPresenter.doPullDownRefresh();
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
            }
        });

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        whitchPage = 1;
                        data.clear();
                        articalListActivityPresenter.doPullDownRefresh();
                    }
                }, 0);
            }
        });
       layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new ArticleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(ArticalListActivity.this,ArticalDetailActivity.class);
                intent.putExtra("articalData",data.get(position));
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                Log.d("test", "StateChanged = " + newState);


            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Log.d("test", "onScrolled");
                loadMoreData();
            }
        });


    }

    //根据位置自动判断是否加载更多数据
    private void loadMoreData(){

        int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
        if (lastVisibleItemPosition + 1 == adapter.getItemCount()) {
            Log.d("test", "loading executed");

            boolean isRefreshing = swipeRefreshLayout.isRefreshing();
            if (isRefreshing) {
                adapter.notifyItemRemoved(adapter.getItemCount());
                return;
            }
            if (!isLoading) {
                isLoading = true;
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        whitchPage +=1;
                        articalListActivityPresenter.doPullUpLoading(whitchPage);
                        Log.d("test", "load more completed");
                        isLoading = false;
                    }
                }, 0);
            }
        }
    }

    @Override
    public void doPullDownRefresh(List<ArticleBean> articalList) {
        data.addAll(articalList);
        adapter.notifyDataSetChanged();
        adapter.setHasMoreData(true);
        swipeRefreshLayout.setRefreshing(false);
        adapter.notifyItemRemoved(adapter.getItemCount());

        //防止页数太少不加载了，滑动的时候会自动判断，判断静止的时候就自己去加载了，0表示不滑动了
        if(recyclerView.getScrollState()==0){
            loadMoreData();
        }
    }

    @Override
    public void doPullUpLoad(List<ArticleBean> articalList) {
        isLoading = false;
        swipeRefreshLayout.setRefreshing(false);
        if(articalList==null||articalList.size()==0){
            adapter.setHasMoreData(false);
        }else{
            data.addAll(articalList);
        }
        adapter.notifyDataSetChanged();
        adapter.notifyItemRemoved(adapter.getItemCount());
    }
}
